package com.example.starapp.parnterfrag;


import androidx.appcompat.app.AppCompatActivity;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.starapp.R;
import com.example.starapp.custom.CircleImageView;
import com.example.starapp.utils.AssetsUtils;
import com.example.starapp.utils.LoadNetDataAsyncTask;
import com.example.starapp.utils.URLUtils;
import com.google.gson.Gson;

import java.util.Map;

public class ParnterAnalysisActivity extends AppCompatActivity implements View.OnClickListener,LoadNetDataAsyncTask.onGetNetDataListener {
    CircleImageView manIv,womanIv;
    ImageView backIv;
    TextView manTv,womanTv,pdTv,vsTv,pfTv,bzTv,jxTv,zyTv,titleTv;
    private String man_name,man_logo,woman_name,woman_logo;
    private String parnterURL;   //需要访问的网址

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parnter_analysis);
        initView();
//        接受上一级界面传值的方法
        getLastData();
//        获取接口
        String manName = man_name.replace("座","");
        String womanName = woman_name.replace("座","");
        parnterURL = URLUtils.getParnterURL(manName, womanName);
//        获取网络数据的方法
        new LoadNetDataAsyncTask(this,this).execute(parnterURL);
    }

    /*
     * 接受上一级页面跳转时，传来的数据*/
    private void getLastData() {
        Intent intent = getIntent();
        man_name = intent.getStringExtra("man_name");
        man_logo = intent.getStringExtra("man_logo");
        woman_name = intent.getStringExtra("woman_name");
        woman_logo = intent.getStringExtra("woman_logo");
//        设置能够显示的控件信息
        Map<String, Bitmap> contentlogoImgMap =
                AssetsUtils.getContentLogoImgMap();
        Bitmap manBm = contentlogoImgMap.get(man_logo);
        manIv.setImageBitmap(manBm);
        Bitmap womanBm = contentlogoImgMap.get(woman_logo);
        womanIv.setImageBitmap(womanBm);
        manTv.setText(man_name);
        womanTv.setText(woman_name);
        pdTv.setText("星座配对-"+man_name+"和"+woman_name+"配对");
        vsTv.setText(man_name+"  vs  "+woman_name);
    }

    /* 初始化控件*/
    private void initView() {
        manIv = findViewById(R.id.partanalysis_iv_man);
        womanIv = findViewById(R.id.partanalysis_iv_woman);
        manTv = findViewById(R.id.partanalysis_tv_man);
        womanTv = findViewById(R.id.partanalysis_tv_woman);
        pdTv = findViewById(R.id.partanalysis_tv_pd);
        vsTv = findViewById(R.id.partanalysis_tv_vs);
        pfTv = findViewById(R.id.partanalysis_tv_pf);
        bzTv = findViewById(R.id.partanalysis_tv_bz);
        jxTv = findViewById(R.id.partanalysis_tv_jx);
        zyTv = findViewById(R.id.partanalysis_tv_zy);
        backIv = findViewById(R.id.title_iv_back);
        titleTv = findViewById(R.id.title_tv);

        titleTv.setText("配对分析");
        backIv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_iv_back:
                finish();  //销毁当前activity，返回上一级页面
                break;
        }
    }

    /* 解析展示数据的函数*/
    private void parseShowData(String json) {
        Gson gson = new Gson();
        ParnterBean parnterBean = gson.fromJson(json, ParnterBean.class);
        ParnterBean.ResultBean resultBean = parnterBean.getResult();

        pfTv.setText("配对评分: "+resultBean.getZhishu()+"  "+resultBean.getJieguo());
        bzTv.setText("星座比重: "+resultBean.getBizhong());
        jxTv.setText("解析:\n\n"+resultBean.getLianai());
        zyTv.setText("注意事项:\n\n"+resultBean.getZhuyi());
    }
    //    接口回调，访问数据成功后，会在此函数返回网络数据
    @Override
    public void showDataFromNet(String s) {
        parseShowData(s);
    }
}